<template>
	<tm-drawer v-model:show="show" hideHeader closeable :height="900">
		<view class="h-full fd-c">
			<scroll-view class="flex1 p-24 fd-c" scroll-y="true">
				<view class="pb-24 fd-r">
					<image
						class="w-140 h-160"
						:src="currentSku.img"
						mode=""
						@click="HPreview(currentSku.img)"
					></image>

					<view class="flex1 pl-24 fd-c">
						<view class="fd-r c-red">
							<text class="fs-24">￥</text>
							<text class="fs-28 fw-b">
								{{ currentSku.price }}
							</text>
						</view>
						<text class="py-12 fs-28">
							已选：{{ currentSku.spec1 }} {{ currentSku.spec2 }}
						</text>
						<tm-stepper
							v-model="count"
							color="red"
							bgColor="red"
							:step="1"
							circular
							:defaultValue="1"
							:min="1"
							:max="currentSku.stock"
						></tm-stepper>
					</view>
				</view>
				<view
					class="pb-16"
					v-for="(spec, idx1) in specList2"
					:key="spec.uid"
				>
					<text class="fs-28">{{ spec.label }}：</text>
					<view class="fd-r fw-w">
						<view
							v-for="(item, idx2) in spec.children"
							:key="item.uid"
							class="mr-16"
							@click="HSkuChange(idx1, item.uid)"
						>
							<button class="btn-tag" v-if="item.disabled">
								{{ item.label }}
							</button>
							<block v-else>
								<button
									class="btn-tag"
									:class="[
										item.checked
											? 'btn-red-plain'
											: 'btn-grey'
									]"
								>
									{{ item.label }}
								</button>
							</block>
						</view>
					</view>
				</view>
			</scroll-view>

			<view class="px-24 py-12">
				<button class="btn-red fs-28" @click="HAdd">确定</button>
			</view>
		</view>
	</tm-drawer>
</template>

<script setup>
import { ref } from 'vue';

const show = ref(false);
const count = ref(1); // 商品数量
const checkData = {
	Id1: null,
	Id2: null
};

// 保留一份旧数据
const specList = ref([]);
// 用于展示的数据
const specList2 = ref([]);
const skuList = ref([]);
const currentSku = ref({});
const open = (obj) => {
	console.log('z-sku', obj);
	
	// 复制一份
	specList.value = JSON.parse(obj.mallGood.specList);
	specList2.value = uni.$tm.u.deepClone(specList.value);
	skuList.value = uni.$tm.u.deepClone(obj.mallGood.skuList).map(v => {
		return {
			...v,
			...JSON.parse(v.attributeJson)
		}
	});
	console.log('skuList.value', skuList.value);
	currentSku.value = uni.$tm.u.deepClone(obj.sku);
	
	// 保存ids
	checkData.id1 = currentSku.value.spec1Id2;
	checkData.id2 = currentSku.value.spec2Id2;
	
	// 去除用不到的tag
	HFilterSpec();
	show.value = true;
};

const HFilterSpec = () => {
	if (specList2.value.length == 1) {
		HFilterSpec1();
	}
	if (specList2.value.length == 2) {
		HFilterSpec2();
	}

	// 设置checked
	HSetChecked();
};

const HFilterSpec1 = () => {
	const arr = skuList.value.map((v) => v.spec1Id2);
	const children = specList2.value[0].children.filter((v) =>
		arr.includes(v.uid)
	);
	specList2.value[0].children = children;
};

const HFilterSpec2 = () => {
	const arr = uni.$tm.u.deepClone(specList.value);
	// 生成第一级数据
	let ids = skuList.value.map((v) => v.spec1Id2);
	let children = arr[0].children.filter((v) => ids.includes(v.uid));
	specList2.value[0].children = children;
	// 生成第二级数据
	ids = skuList.value
		.filter((v) => v.spec1Id2 == currentSku.value.spec1Id2)
		.map((v) => v.spec2Id2);
	children = arr[1].children.filter((v) => ids.includes(v.uid));
	specList2.value[1].children = children;
	
	console.log('specList2.value', specList2.value);
};

const HSetChecked = () => {
	specList2.value[0].children.forEach((v, idx) => {
		if (v.uid == currentSku.value.spec1Id2) {
			v.checked = true;
		}
	});
	// 如果存在2级
	if (specList2.value[1]) {
		specList2.value[1].children.forEach((v, idx) => {
			if (v.uid == currentSku.value.spec2Id2) {
				v.checked = true;
			}
		});
	}
};

const HSkuChange = (idx, id) => {
	if (idx == 0) {
		checkData.id1 = id;
		// 判断原本的规格2id是否存在，不存在选择存在中的第一个
		const ids = skuList.value
			.filter((v) => v.spec1Id2 == checkData.id1)
			.map((v) => v.spec2Id2);
		if (!ids.includes(checkData.id2)) {
			checkData.id2 = ids[0];
		}
	}
	if (idx == 1) {
		checkData.id2 = id;
	}
	currentSku.value = skuList.value.find(
		(v) => v.spec1Id2 == checkData.id1 && v.spec2Id2 == checkData.id2
	);
	HFilterSpec();
};

const HPreview = (url) => {
	uni.previewImage({
		urls: [url]
	});
};

import { useCartStore } from '@/store/cart.js';
const cartStore = useCartStore();
const HAdd = () => {
	const params ={
		goodId: currentSku.value.goodId,
		skuId: currentSku.value.id,
		price: currentSku.value.price,
		quantity: count.value
	}
	cartStore.addCart(params);
	show.value = false;
};

defineExpose({ open }); // 暴露open方法给父组件调用
</script>

<style scoped>
.btn-tag {
	height: 52rpx;
	font-size: 22rpx;
	line-height: 48rpx;
	border-radius: 30rpx;
	padding: 0 40rpx;
	margin-top: 16rpx;
}

.btn-grey {
	background-color: #f5f5f5;
	color: #333;
}
</style>
